---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'EKS Cluster definition.'

Parameters:

  VpcId:
    Type: String
    Description: VPC ID.

  Subnets:
    Type: CommaDelimitedList
    Description: List of subnets in the VPC.

  ClusterName:
    Type: String
    Description: EKS Kubernetes cluster name.

  KubernetesVersion:
    Type: String
    Description: EKS Kubernetes cluster version.

Resources:

  ClusterRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          Effect: Allow
          Principal:
            Service:
              - eks.amazonaws.com
          Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AmazonEKSClusterPolicy
        - arn:aws:iam::aws:policy/AmazonEKSServicePolicy

  ClusterControlPlaneSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Cluster communication with worker nodes.
      VpcId: !Ref VpcId

  Cluster:
    Type: "AWS::EKS::Cluster"
    Properties:
      Name: !Ref ClusterName
      Version: !Ref KubernetesVersion
      RoleArn: !GetAtt ClusterRole.Arn
      ResourcesVpcConfig:
        SecurityGroupIds:
          - !Ref ClusterControlPlaneSecurityGroup
        SubnetIds: !Ref Subnets

Outputs:

  ClusterName:
    Value: !Ref ClusterName
    Description: Cluster Name
    Export:
      Name:
        Fn::Sub: "${AWS::StackName}-ClusterName"

  ClusterEndpoint:
    Value: !GetAtt Cluster.Endpoint
    Description: Cluster Endpoint
    Export:
      Name:
        Fn::Sub: "${AWS::StackName}-ClusterEndpoint"
